﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ninject;
using Legazpi.Payroll.BusinessLogic.Services.Interfaces;
using Legazpi.Payroll.DataAccess.DataObjects;
using Web.Codes;
using Web.Utils;

namespace Legazpi.Payroll.Web.Admin.Payroll
{
    public partial class DeathContribution : System.Web.UI.Page
    {
        [Inject]
        public IContributionService _contributionService { get; set; }
        [Inject]
        public IPayrollDateService _payrollDateService { get; set; }

        public bool IsSuccess { get; set; }
        public string NotifyText { get; set; }

        private int _contributionId;
        public int CurrentContributionId
        {
            get
            {
                int id;
                return int.TryParse(ContributionId.Value, out id) ? id : 0;
            }
            set
            {
                _contributionId = value;
                ContributionId.Value = _contributionId.ToString();
            }
        }
        private int _currentPaydateId;
        public int CurrentPaydateId
        {
            get
            {
                int id;
                return int.TryParse(PayrollPeriodDdl.SelectedValue.ToString(), out id) ? id : 0;
            }
            set
            {                
                _currentPaydateId = value;                
                var item = PayrollPeriodDdl.Items.FindByValue(_currentPaydateId.ToString());
                if (item != null)
                {
                    PayrollPeriodDdl.ClearSelection();
                    item.Selected = true;
                }                
            }
        }

        #region Implementations
        void BindGrid()
        {
            repeater.DataSource = _contributionService.GetListByPayperiod(CurrentPaydateId);
            repeater.DataBind();
        }
        void Delete(int id)
        {
            _contributionService.Delete(id);
            IsSuccess = _contributionService.Save();
            CurrentContributionId = 0;
            NotifyText = "Successfully Deleted.";
            BindGrid();
        }

        void Edit(int id)
        {
            var contribution = _contributionService.GetById(id);
            if (contribution == null)
                return;

            ObjectToData(contribution);

            CurrentContributionId = contribution.Id;            
            SubmitBtn.Text = "Update";
        }

        void Update(int id)
        {
            var contribution = _contributionService.GetById(id);
            if (contribution == null)
            {
                Add();
                return;
            }
            DataToObject(ref contribution);

            _contributionService.Update(contribution);            
            IsSuccess = _contributionService.Save();

            CurrentContributionId = contribution.Id;
            NotifyText = "Successfully Updated Record.";
        }
        void Add()
        {
            var contribution = new Contribution();

            DataToObject(ref contribution);

            _contributionService.Add(contribution);
            IsSuccess = _contributionService.Save();

            CurrentContributionId = contribution.Id;
            NotifyText = "Successfully Added New Record.";
            SubmitBtn.Text = "Update";

        }
        void ClearForm()
        {
            ClearControl(this.Page);
            CurrentContributionId = 0;
            SubmitBtn.Text = "Submit";
        }
        void ClearControl(Control ctrl)
        {
            foreach (Control _ctrl in ctrl.Controls)
            {
                if (_ctrl.GetType() == typeof(TextBox))
                {
                    ((TextBox)_ctrl).Text = string.Empty;
                }
                ClearControl(_ctrl);
            }
        }
        void Save()
        {
            if (CurrentContributionId == 0)
                Add();
            else
                Update(CurrentContributionId);
        }
        void DataToObject(ref Contribution obj)
        {
            double amount;
            double.TryParse(AmountTxt.Text, out amount);
            obj.Description = DescTxt.Text;
            obj.Amount = amount;
            obj.PayDateId = CurrentPaydateId;
        }

        void ObjectToData(Contribution obj)
        {
            DescTxt.Text = obj.Description;
            AmountTxt.Text = obj.Amount.ToString();
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                var activePayrollDate = _payrollDateService.GetActivePayroll();
                PayrollPeriodDdl.ToPayrollPeriodSelectList(activePayrollDate != null ? activePayrollDate.Id : 0);
                BindGrid();
            }                
        }

        protected void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int intTemp = 0;
            if (int.TryParse((string)e.CommandArgument, out intTemp))
            {
                switch (e.CommandName.ToLower())
                {
                    case Constants.Update_Command:
                        Edit(intTemp);
                        break;
                    case Constants.Delete_Command:
                        Delete(intTemp);
                        break;
                }
            }
        }

        protected void SubmitBtn_Click(object sender, EventArgs e)
        {
            Save();
            if (IsSuccess)
            {
                ClearForm();
                BindGrid();
            }
        }

        protected void CancelBtn_Click(object sender, EventArgs e)
        {
            ClearForm();
        }

        protected void PayrollPeriodDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGrid();
            ClearForm();
        }
    }
}